﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NCRGEA.UserControls
{
    public partial class ucDeductions : UserControl
    {
        private bool INITING = false;

        private int _MemberID;
        public int MemberID
        {
            get
            {
                return _MemberID;
            }
            set
            {
                _MemberID = value;
                RefreshData();
            }
        }

        public ucDeductions()
        {
            InitializeComponent();
        }

        public void PopulateAmounts()
        {
            string sqlQry = @"SELECT
	                            DuesID,
	                            Description + ' - $' + CAST(CAST(Amount as NUMERIC(10,2)) as VARCHAR) Description,
	                            Amount
                            FROM
	                            memDues
                            WHERE
	                            Description='Monthly Dues'";
            DataTable result = AAL.DAL.ExecuteQuery(sqlQry);
            if (result != null)
            {
                cmbAmount.DataSource = result;
                cmbAmount.DisplayMember = "Description";
                cmbAmount.ValueMember = "DuesID";
            }
        }

        public void PopulateFlag()
        {
            string DedFlag = "";
            string sqlQry = @"SELECT DedFlag FROM memInfo WHERE MemberID=@MemberID";
            SqlParameter[] sParamArr = new SqlParameter[1];
            sParamArr[0] = new SqlParameter("@MemberID", MemberID) { SqlDbType = SqlDbType.Int };
            DataTable tbl = AAL.DAL.ExecuteQuery(sqlQry, sParamArr);
            if (tbl != null && tbl.Rows.Count == 1)
                if (tbl.Rows[0]["DedFlag"] != DBNull.Value && tbl.Rows[0]["DedFlag"] != null)
                    DedFlag = tbl.Rows[0]["DedFlag"].ToString();
            lblFlag.Text = DedFlag;
        }

        public void PopulateTotalDeductions()
        {
            string sqlQry = @"SELECT
	                                '$' + CAST(CAST(SUM(Amount) as NUMERIC(10,2)) AS VARCHAR) Amount
                                FROM (
	                                SELECT
		                                COALESCE(md.Amount,0) Amount
	                                FROM
		                                memInfo mi
		                                LEFT JOIN
			                                memDues md
		                                ON
			                                mi.DuesID=md.DuesID
	                                WHERE
		                                MemberID=@MemberID
		
	                                UNION ALL
		
	                                SELECT
		                                COALESCE(mpr.Amount,0) Amount
	                                FROM
		                                memPlans mp
		                                INNER JOIN
			                                memPrograms mpr
		                                ON
			                                mp.ProgramID=mpr.ProgramID
	                                WHERE
		                                mp.MemberID=@MemberID) f";
            SqlParameter[] sParamArr = new SqlParameter[1];
            sParamArr[0] = new SqlParameter("@MemberID", MemberID) { SqlDbType = SqlDbType.Int };
            DataTable tbl = AAL.DAL.ExecuteQuery(sqlQry, sParamArr);
            if (tbl != null && tbl.Rows.Count == 1)
                txtTotal.Text = tbl.Rows[0]["Amount"].ToString();
            else
                txtTotal.Text = "$0.00";
        }

        public void PopulatePrograms()
        {
            string sqlQry = @"SELECT
	                                mpr.Description Program,
	                                '$' + CAST(CAST(mpr.Amount as NUMERIC(10,2)) as VARCHAR) Amount
                                FROM
	                                memPlans mp
	                                INNER JOIN
		                                memPrograms mpr
	                                ON
		                                mp.ProgramID=mpr.ProgramID
                                WHERE
	                                mp.MemberID=@MemberID AND
	                                mp.Active=1";
            SqlParameter[] sParamArr = new SqlParameter[1];
            sParamArr[0] = new SqlParameter("@MemberID", MemberID) { SqlDbType = SqlDbType.Int };
            DataTable tbl = AAL.DAL.ExecuteQuery(sqlQry, sParamArr);
            grdPrograms.DataSource = tbl;
        }

        public void RefreshData()
        {
            INITING = true;
            PopulateAmounts();
            PopulateFlag();
            PopulateTotalDeductions();
            PopulatePrograms();

            bool enrolled = AAL.UAL.IsMemberEnrolled(_MemberID);
            UpdateUI(enrolled);
            INITING = false;
        }

        private void UpdateUI(bool enrolled)
        {
            chkEnrolled.Checked = enrolled;
            groupBox1.Enabled = enrolled;
            groupBox2.Enabled = enrolled;
            lblDeductions.Enabled = enrolled;
            lblFlag.Enabled = enrolled;
            lblFlagLabel.Enabled = enrolled;
            txtTotal.Enabled = enrolled;
        }

        private void chkEnrolled_CheckedChanged(object sender, EventArgs e)
        {
            if (INITING)
                return;

            if (chkEnrolled.Checked == true)
            {
                if (!AAL.UAL.ValidateSSN_Enrollment(AAL.UAL.GetSSN(MemberID)))
                {
                    MessageBox.Show("This member needs a valid SSN before they are eligible to enroll in deductions!", "NCRGEA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    RefreshData();
                }
                else
                {
                    //Enroll the member
                    AAL.UAL.EnrollMember(MemberID);
                    RefreshData();
                }
            }
            else
            {
                //Trying to unenroll
                if (AAL.UAL.CanUnenroll(_MemberID))
                {
                    AAL.UAL.UnenrollMember(_MemberID);
                    RefreshData();
                }
                else
                {
                    MessageBox.Show("This member cannot unenroll at this time! Please terminate all programs for this member and try again.", "NCRGEA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    RefreshData();
                }
            }
        }
    }
}
